"""Book model definition."""

from __future__ import annotations

from sqlalchemy import String, Text, Integer, DateTime, func, JSON, Column
from app.database import Base


class Book(Base):
    __tablename__ = "books"

    id = Column(Integer, primary_key=True, index=True)
    book_id = Column(String(64), unique=True, index=True)
    title = Column(String(256), index=True, nullable=False)
    subtitle = Column(String(256), nullable=True)
    author = Column(String(128), index=True, nullable=False)
    dynasty = Column(String(64), index=True, nullable=False)
    category = Column(String(64), index=True, nullable=False)
    description = Column(Text, nullable=True)
    total_chapters = Column(Integer, default=0, nullable=False)
    total_characters = Column(Integer, default=0, nullable=False)
    cover = Column(String(512), nullable=True)
    difficulty = Column(String(32), default="中等", nullable=False)
    language = Column(String(32), default="古汉语", nullable=False)
    tags = Column(JSON, nullable=True)
    content = Column(JSON, nullable=True)
    created_at = Column(
        DateTime(timezone=True), server_default=func.now(), nullable=False
    )
    updated_at = Column(
        DateTime(timezone=True),
        server_default=func.now(),
        onupdate=func.now(),
        nullable=False,
    )

    def __repr__(self) -> str:  # pragma: no cover
        return f"<Book id={self.id} book_id={self.book_id} title={self.title}>"
